<?php

use Drupal\mxm_article\Entity\MxmArticle;

function mxm_article_mxm_article_entity_form($form, &$form_state, MxmArticle $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'mxm_article') . '/mxm_article.forms.inc';
  $form_state[MxmArticle::class] = $e;

  $form['title'] = [
    '#type' => 'textfield',
    '#title' => t('标题'),
    '#default_value' => $e->title,
    '#required' => TRUE,
  ];
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => t('启用'),
    '#default_value' => $e->status,
  ];
  $form['sort'] = [
    '#type' => 'textfield',
    '#title' => t('排序'),
    '#default_value' => $e->sort,
    '#required' => TRUE,
    '#element_validate' => ['element_validate_integer'],
  ];
  //  $form['promote'] = [
  //    '#type' => 'checkbox',
  //    '#title' => t('转运须知显示'),
  //    '#default_value' => $e->promote,
  //  ];
  //  $form['announcement'] = [
  //    '#type' => 'checkbox',
  //    '#title' => t('显示在公告'),
  //    '#default_value' => $e->announcement,
  //  ];
  $form['window_index'] = [
    '#type' => 'checkbox',
    '#title' => t('关于我们'),
    '#default_value' => $e->window_index,
  ];
  $form['summary'] = [
    '#title' => t('摘要'),
    '#type' => 'textarea',
    '#default_value' => $e->summary,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function mxm_article_mxm_article_entity_form_submit($form, &$form_state) {
  $e = $form_state[MxmArticle::class];
  if ($e instanceof MxmArticle) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function mxm_article_mxm_article_delete_form($form, &$form_state, MxmArticle $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'mxm_article') . '/mxm_article.forms.inc';
  $form_state[MxmArticle::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/mxm_article/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function mxm_article_mxm_article_delete_form_submit($form, &$form_state) {
  $e = $form_state[MxmArticle::class];
  if ($e instanceof MxmArticle) {
    $e->delete();
  }
  $form_state['redirect'] = 'admin/content/mxm_article';
}

